using System;
using System.Diagnostics;
using NUnit.Framework;
using Pagination.Storage;

namespace Tests.Pagination.Storage.Stress
{
    public abstract class StorageStressTestBase : TestBase
    {
        [SetUp]
        public override void SetUp()
        {
            base.SetUp();
            Storage = NewStorage();
        }

        protected IStorage Storage { get; private set; }

        protected abstract IStorage NewStorage();


        protected void AddRandomElements(int count)
        {
            var random = new Random();
            for (var element = 0; element < count; ++element)
                Storage.Add(random.Next());
        }

        protected void AddSequentialElements(int count)
        {
            for (var element = 0; element < count; ++element)
                Storage.Add(element);
        }

        protected void AddOrderedByDescendingSequentialElements(int count)
        {
            for (var element = count; element > 0; --element)
                Storage.Add(element);
        }

        protected static long WrapWithTimerAndGetElapsedMilliseconds(Action action)
        {
            var watch = new Stopwatch();
            watch.Start();
            action();
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
    }
}